package com.dianping.cat.consumer.state.model.transform;

import com.dianping.cat.consumer.state.model.IVisitor;
import com.dianping.cat.consumer.state.model.entity.Detail;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.Message;
import com.dianping.cat.consumer.state.model.entity.ProcessDomain;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  22 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  26 */     this.m_out = new DataOutputStream(out);
/*  27 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(StateReport stateReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  33 */     build(stateReport, out);
/*  34 */     return out.toByteArray();
  }


/*  38 */   public static void build(StateReport stateReport, OutputStream out) { stateReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDetail(Detail detail) {
/*  43 */     if (detail.getId() != null) {
/*  44 */       writeTag(1, 1);
/*  45 */       writeLong(detail.getId().longValue());
    }

/*  48 */     writeTag(2, 0);
/*  49 */     writeLong(detail.getTotal());

/*  51 */     writeTag(3, 0);
/*  52 */     writeDouble(detail.getSize());

/*  54 */     writeTag(4, 0);
/*  55 */     writeLong(detail.getTotalLoss());

/*  57 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/*  62 */     if (machine.getIp() != null) {
/*  63 */       writeTag(1, 1);
/*  64 */       writeString(machine.getIp());
    }

/*  67 */     writeTag(2, 0);
/*  68 */     writeLong(machine.getTotal());

/*  70 */     writeTag(3, 0);
/*  71 */     writeLong(machine.getTotalLoss());

/*  73 */     writeTag(4, 0);
/*  74 */     writeDouble(machine.getMaxTps());

/*  76 */     writeTag(5, 0);
/*  77 */     writeDouble(machine.getAvgTps());

/*  79 */     writeTag(6, 0);
/*  80 */     writeLong(machine.getBlockTotal());

/*  82 */     writeTag(7, 0);
/*  83 */     writeLong(machine.getBlockLoss());

/*  85 */     writeTag(8, 0);
/*  86 */     writeLong(machine.getBlockTime());

/*  88 */     writeTag(9, 0);
/*  89 */     writeLong(machine.getPigeonTimeError());

/*  91 */     writeTag(10, 0);
/*  92 */     writeLong(machine.getNetworkTimeError());

/*  94 */     writeTag(11, 0);
/*  95 */     writeLong(machine.getDump());

/*  97 */     writeTag(12, 0);
/*  98 */     writeLong(machine.getDumpLoss());

/* 100 */     writeTag(13, 0);
/* 101 */     writeDouble(machine.getSize());

/* 103 */     writeTag(14, 0);
/* 104 */     writeDouble(machine.getDelaySum());

/* 106 */     writeTag(15, 0);
/* 107 */     writeDouble(machine.getDelayAvg());

/* 109 */     writeTag(16, 0);
/* 110 */     writeInt(machine.getDelayCount());

/* 112 */     if (!machine.getProcessDomains().isEmpty()) {
/* 113 */       writeTag(33, 2);
/* 114 */       writeInt(machine.getProcessDomains().size());

/* 116 */       for (ProcessDomain processDomain : machine.getProcessDomains().values()) {
/* 117 */         processDomain.accept(this.m_visitor);
      }
    }

/* 121 */     if (!machine.getMessages().isEmpty()) {
/* 122 */       writeTag(34, 2);
/* 123 */       writeInt(machine.getMessages().size());

/* 125 */       for (Message message : machine.getMessages().values()) {
/* 126 */         message.accept(this.m_visitor);
      }
    }

/* 130 */     writeTag(63, 3);
  }


  public void visitMessage(Message message) {
/* 135 */     if (message.getId() != null) {
/* 136 */       writeTag(1, 1);
/* 137 */       writeLong(message.getId().longValue());
    }

/* 140 */     if (message.getTime() != null) {
/* 141 */       writeTag(2, 1);
/* 142 */       writeDate(message.getTime());
    }

/* 145 */     writeTag(3, 0);
/* 146 */     writeLong(message.getTotal());

/* 148 */     writeTag(4, 0);
/* 149 */     writeLong(message.getTotalLoss());

/* 151 */     writeTag(5, 0);
/* 152 */     writeLong(message.getDump());

/* 154 */     writeTag(6, 0);
/* 155 */     writeLong(message.getDumpLoss());

/* 157 */     writeTag(7, 0);
/* 158 */     writeDouble(message.getSize());

/* 160 */     writeTag(8, 0);
/* 161 */     writeDouble(message.getDelaySum());

/* 163 */     writeTag(9, 0);
/* 164 */     writeInt(message.getDelayCount());

/* 166 */     writeTag(10, 0);
/* 167 */     writeLong(message.getPigeonTimeError());

/* 169 */     writeTag(11, 0);
/* 170 */     writeLong(message.getNetworkTimeError());

/* 172 */     writeTag(12, 0);
/* 173 */     writeLong(message.getBlockTotal());

/* 175 */     writeTag(13, 0);
/* 176 */     writeLong(message.getBlockLoss());

/* 178 */     writeTag(14, 0);
/* 179 */     writeLong(message.getBlockTime());

/* 181 */     writeTag(63, 3);
  }


  public void visitProcessDomain(ProcessDomain processDomain) {
/* 186 */     if (processDomain.getName() != null) {
/* 187 */       writeTag(1, 1);
/* 188 */       writeString(processDomain.getName());
    }

/* 191 */     if (processDomain.getIps() != null) {
/* 192 */       writeTag(2, 2);
/* 193 */       writeInt(processDomain.getIps().size());

/* 195 */       for (String ip : processDomain.getIps()) {
/* 196 */         writeString(ip);
      }
    }

/* 200 */     writeTag(3, 0);
/* 201 */     writeLong(processDomain.getTotal());

/* 203 */     writeTag(4, 0);
/* 204 */     writeLong(processDomain.getTotalLoss());

/* 206 */     writeTag(5, 0);
/* 207 */     writeDouble(processDomain.getSize());

/* 209 */     writeTag(6, 0);
/* 210 */     writeDouble(processDomain.getAvg());

/* 212 */     if (!processDomain.getDetails().isEmpty()) {
/* 213 */       writeTag(33, 2);
/* 214 */       writeInt(processDomain.getDetails().size());

/* 216 */       for (Detail detail : processDomain.getDetails().values()) {
/* 217 */         detail.accept(this.m_visitor);
      }
    }

/* 221 */     writeTag(63, 3);
  }


  public void visitStateReport(StateReport stateReport) {
/* 226 */     writeTag(63, 0);

/* 228 */     if (stateReport.getDomain() != null) {
/* 229 */       writeTag(1, 1);
/* 230 */       writeString(stateReport.getDomain());
    }

/* 233 */     if (stateReport.getStartTime() != null) {
/* 234 */       writeTag(2, 1);
/* 235 */       writeDate(stateReport.getStartTime());
    }

/* 238 */     if (stateReport.getEndTime() != null) {
/* 239 */       writeTag(3, 1);
/* 240 */       writeDate(stateReport.getEndTime());
    }

/* 243 */     if (!stateReport.getMachines().isEmpty()) {
/* 244 */       writeTag(33, 2);
/* 245 */       writeInt(stateReport.getMachines().size());

/* 247 */       for (Machine machine : stateReport.getMachines().values()) {
/* 248 */         machine.accept(this.m_visitor);
      }
    }

/* 252 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 257 */       writeVarint(value.getTime());
/* 258 */     } catch (Exception e) {
/* 259 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 265 */       writeVarint(Double.doubleToLongBits(value));
/* 266 */     } catch (Exception e) {
/* 267 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 273 */       writeVarint(value & 0xFFFFFFFFL);
/* 274 */     } catch (Exception e) {
/* 275 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 281 */       writeVarint(value);
/* 282 */     } catch (Exception e) {
/* 283 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 289 */       this.m_out.writeUTF(value);
/* 290 */     } catch (IOException e) {
/* 291 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 297 */       this.m_out.writeByte((field << 2) + type);
/* 298 */     } catch (IOException e) {
/* 299 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 305 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 306 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 309 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 310 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/state/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */